var ModuleUtil = {
  getOutOrderFrameColor: function (orderList) {
    var ringMaterialTypeList = ["金属圈", "板材圈", "TR圈", "PC圈", "塑钢圈", "注塑圈", "TLL圈"];
    var legMaterialTypeList = ["金属肶", "板材肶", "TR肶", "PC肶", "塑钢肶", "注塑腿", "TLL肶"];
    orderList = _.filter(orderList, function (order) {
      var color = order.color;
      if (SysUtil.isEmpty(color)) {
        return false;
      }
      var maching = order.maching;
      return ringMaterialTypeList.indexOf(maching) !== -1 ||
        legMaterialTypeList.indexOf(maching) !== -1;
    });

    orderList = _.orderBy(orderList, [function (order) {
      var maching = order.maching;
      if (ringMaterialTypeList.indexOf(maching) !== -1) {
        return 0;
      }
      return 1;
    }], ['asc'])

    var colorList = _.map(orderList, 'color');
    return colorList.join('+');
  },

  getMaterialMachingMap: function () {
    return ajaxRequest('structureTree/getHalfMaterialNameList').then(function (machineTypeList) {
      var materialMachingMap = {};
      for (var i = 0; i < machineTypeList.length; i++) {
        var machineType = machineTypeList[i];
        machineType.id = Number(machineType.id);
        materialMachingMap[machineType.id] = machineType.halfMaterialName
      }
      return materialMachingMap;
    });
  },

  getMaterialMachingInfoMap: function () {
    return ajaxRequest('structureTree/getHalfMaterialTypeList').then(function (materialTypeList) {
      var materialTypeGroup = _.groupBy(materialTypeList, 'nameId');
      var materialMachingMap = {};
      for (var nameId in materialTypeGroup) {
        var list = materialTypeGroup[nameId];
        var obj = {
          name: list[0].halfMaterialName,
          materialTypeList: _.map(list, 'materialType')
        };
        materialMachingMap[nameId] = obj;
      }
      return materialMachingMap;
    });
  },

  getOutOrderData: function (postData, isSearch) {
    var promiseList = [ajaxRequest('putOutNew/getPutoutOrderGroupType', postData)];
    if (isSearch) {
      var sumPostData = _.cloneDeep(postData);
      delete sumPostData.start;
      delete sumPostData.length;
      promiseList.push(ajaxRequest('putOutNew/getPutOutOrderSumPrice', sumPostData));
    }
    return Promise.all(promiseList).then(function (res) {
      var orderData = res[0];
      var sumPrice = res[1];
      if (sumPrice !== undefined) {
        orderData.sumPrice = sumPrice;
      }
      return orderData;
    });
  },

  getEleOrderData: function (orderId) {
    return ajaxRequest('putOutNew/getDianDuColorList', {
      orderId: orderId
    }).then(function (res) {
      var eleColorList = res.dianDuColorList;
      var eleColors = [];
      for (var i = 0; i < eleColorList.length; i++) {
        var eleColor = eleColorList[i];
        var colorDescription = eleColor.colorDescription;
        colorDescription = colorDescription.trim();
        if (colorDescription === '') {
          continue;
        }

        var orderTotal = eleColor.orderTotal;
        if (SysUtil.isEmpty(orderTotal)) {
          orderTotal = undefined;
        } else {
          orderTotal = Number(orderTotal);
        }
        eleColors.push({
          lineNo: eleColor.lineNo,
          materialType: eleColor.materialType,
          count: orderTotal,
          color: colorDescription
        });
      }
      var eleColorObj = _.groupBy(eleColors, 'materialType');

      var existMaterialList = res.aleradyPutOutMaterialList;
      var material = {};
      for (var i = 0; i < existMaterialList.length; i++) {
        var existMaterial = existMaterialList[i];
        var additionalMaterialType = existMaterial.additionalMaterialType;
        if (additionalMaterialType !== '') {
          material[existMaterial.materialType] = additionalMaterialType;
        }
      }

      return {
        eleColorObj: eleColorObj,
        material: material
      };
    });
  },

  getEleOrderSelect: function (detailList, maching, eleOrderData) {
    detailList = _.filter(detailList, {
      lineNo: '-1'
    });
    if (detailList.length === 0) {
      return {
        hasEle: false,
        canEle: false
      };
    }

    var material = eleOrderData.material;
    if (maching === 'all' || maching.indexOf('machineMap-') === 0) {
      for (var i = 0; i < detailList.length; i++) {
        var detail = detailList[i];
        var materialType = detail.materialType;
        if (material[materialType] !== undefined) {
          return {
            hasEle: true,
            canEle: false
          }
        }
      }

      return {
        hasEle: false,
        canEle: true
      };
    }

    var canEle, hasEle;
    var elec = material[maching];
    if (elec === undefined) {
      canEle = true;
      hasEle = false;
    } else {
      canEle = false;
      hasEle = true;
    }

    return {
      hasEle: hasEle,
      canEle: canEle
    };
  }
};